package com.cdtu.labpreview.controller;

import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.controller.vo.ContentPreviewVo;
import com.cdtu.labpreview.entity.BaseIntroduction;
import com.cdtu.labpreview.entity.ContentPreview;
import com.cdtu.labpreview.mapper.ContentPreviewMapper;
import com.cdtu.labpreview.service.IContentPreviewService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 实验内容预习模块(针对实验中的难点予以提示)
 */
@RestController
@RequestMapping("/preview")
@Api(tags = "实验内容预习模块")
public class ContentPreviewController {

    @Autowired
    private IContentPreviewService contentPreviewService;

    @Autowired
    private ContentPreviewMapper contentPreviewMapper;

    /**
     * 老师获取自己已发布的实验内容预习
     * @return
     */
    @GetMapping("/teacher")
    public Result getBaseIntroductionOfTeacher(){
        int tno = UserHolder.getUser().getUserNo();//需要拦截器拦截保存用户信息
        return contentPreviewService.getContentPreviewOfTeacher(tno);
    }

    /**
     * 学生获取本班级对应的所有任科老师发布的实验内容预习
     * @return
     */
    @GetMapping("/student")
    public Result getContentPreview(){
        int sno = UserHolder.getUser().getUserNo();
        return contentPreviewService.getContentPreviewOfStudent(sno);
    }

    /**
     * 新增实验内容预习
     * @param contentPreviewVo
     * @return
     */
    @PostMapping
    public Result addContentPreview(@RequestBody ContentPreviewVo contentPreviewVo){
        ContentPreview contentPreview=new ContentPreview();
        BeanUtils.copyProperties(contentPreviewVo,contentPreview);
        contentPreview.setTno(UserHolder.getUser().getUserNo());
        int flag = contentPreviewMapper.insert(contentPreview);
        return flag>0 ? Result.ok() : Result.fail("添加失败！");
    }

    /**
     * 修改实验内容预习
     * @param contentPreview
     * @return
     */
    @PutMapping
    public Result updateContentPreview(@RequestBody ContentPreview contentPreview){
        int flag = contentPreviewMapper.updateById(contentPreview);
        return flag>0 ? Result.ok() : Result.fail("添加失败！");
    }

    /**
     * 删除实验内容预习
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteContentPreview(@PathVariable("id") long id){
        int flag = contentPreviewMapper.deleteById(id);
        return flag>0 ? Result.ok() : Result.fail("添加失败！");
    }

}
